package com.woniu.controller;

import com.github.pagehelper.PageInfo;
import com.woniu.common.CommonResult;
import com.woniu.dto.ActivityDTO;
import com.woniu.service.ActivityService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;
@Controller
@RequestMapping("/activity")
@Api(tags = "活动审批模块")
public class ActivityController {

    @Autowired
    private ActivityService activityService;

    /**
     * 分页查询活动审批数据
     * @param currentPage
     * @param pageSize
     * @return
     */
    @RequestMapping("/list")
    @ResponseBody
    @RequiresPermissions("sys:al:list")
    @ApiOperation(value = "活动审批分页查询")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "currentPage",value = "当前页数",defaultValue = "1"),
            @ApiImplicitParam(name = "pageSize",value = "每页数据条数",defaultValue = "5")
    })
    public CommonResult pmList(@RequestParam(value = "page",defaultValue = "1",required = false)Integer currentPage,
                               @RequestParam(value = "limit",defaultValue = "5",required = false)Integer pageSize){
        PageInfo info = null;
        try {
            List<ActivityDTO> list=activityService.findListByPage(currentPage,pageSize);
            info=new PageInfo(list);
            return CommonResult.success(info);
        } catch (Exception e) {
            e.printStackTrace();
            return CommonResult.failed();
        }

    }

    /**
     * 修改审批状态
     * @param id
     * @return
     */
    @PutMapping("/confirm/{id}")
    @ResponseBody
    @RequiresPermissions("sys:al:examine")
    @ApiOperation(value = "活动审批")
    @ApiImplicitParam(name = "id",value = "活动编号id值",dataType = "Integer")
    public CommonResult confirmPM(@PathVariable("id") Integer id){
        System.out.println("活动确认审核"+id);
        Date examineTime=new Date();
        try {
            activityService.editStatusById(id,examineTime);
        } catch (Exception e) {
            e.printStackTrace();
            CommonResult.failed("审批失败");
        }
        return CommonResult.success("审批成功");
    }

    /**
     * 页面查询功能
     * @param name
     * @param status
     * @return
     */
    @RequestMapping("/select")
    @ResponseBody
    @ApiOperation(value = "活动审批页面搜索")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "name",value = "用户名称"),
            @ApiImplicitParam(name = "status",value = "审批状态",dataType = "Integer")
    })
    public CommonResult select(String name,Integer status){
        PageInfo info=null;
        try {
            List<ActivityDTO> list=activityService.select(name,status);
            info=new PageInfo(list);
            return CommonResult.success(info);
        } catch (Exception e) {
            e.printStackTrace();
            return CommonResult.failed();
        }
    }

    /**
     * 拒绝审批操作
     * @param id
     * @return
     */
    @RequestMapping("/refuse/{id}")
    @ResponseBody
    @ApiOperation(value = "拒绝审批")
    @ApiImplicitParam(name = "id",value = "活动编号id值",dataType = "Integer")
    public CommonResult refuse(@PathVariable("id")Integer id){
        //获取当前时间
        Date examineTime=new Date();
        try {
            activityService.refuse(id,examineTime);
            return CommonResult.success("操作成功！");
        } catch (Exception e) {
            e.printStackTrace();
            return CommonResult.failed("操作失败！");
        }
    }
}
